package com.itheima;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.function.Consumer;

/**
 * @ProjectName: ybxy6
 * @Titile: HashSetDemo
 * @Author: Muelsyse
 * @Description: TODO
 */
public class HashSetDemo {
    public static void main(String[] args) {
        // 创建一个ArrayList对象并添加重复元素
        ArrayList<String> list = new ArrayList<>();
        list.add("帅");
        list.add("帅");
        list.add("帅");
        list.add("帅");
        System.out.println(list);
        // 使用HashSet去重并将结果存入新的ArrayList
        ArrayList<String> newList = new ArrayList<>((new HashSet<>(list)));
        System.out.println(newList);

        // 创建HashSet对象并添加元素
        HashSet<String> hashSet = new HashSet<>();
        hashSet.add("apple");
        hashSet.add("banana");
        hashSet.add("orange");
        hashSet.add("pineapple");
        System.out.println(hashSet);
        // 通过迭代器遍历HashSet并打印每个元素
        Iterator<String> iterator = hashSet.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            System.out.println(next);

            // 通过增强for循环遍历HashSet并打印每个元素
            for (String name : hashSet) {
                System.out.println("name=" + name);
            }
            int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
            for (int i : arr) {
                System.out.println(i);
            }

            // 使用Lambda表达式遍历HashSet并打印每个元素
            hashSet.forEach(new Consumer<String>() {
                @Override
                public void accept(String s) {
                    System.out.println(s);
                }
            });
            hashSet.forEach(s -> System.out.println(s));
            hashSet.forEach(System.out::println);

            // 创建一个Student对象并打印
            Student student = new Student("尤五", 20);
            System.out.println(student);
        }
    }
}
